package com.example.demodatajpa.controller;

import com.example.demodatajpa.dao.BookDao;
import com.example.demodatajpa.entity.Book;
import com.example.demodatajpa.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/books")
public class BookController {
    @Autowired
    BookService bookService;

    @PostMapping("/")
    public Book addBook(@RequestBody Book book){
        bookService.addBook(book);
        return book;
    }

    @PostMapping("/all")
    public List<Book> addAllBook(@RequestBody List<Book> books){
        bookService.addAllBook(books);
        return books;
    }

    @DeleteMapping("/delete/{id}")
    public String deleteByIdBook(@PathVariable Long id){
        bookService.deleteByIdBook(id);
        return "成功";
    }

    @DeleteMapping("/deleteAll")
    public String deleteAllBook(@RequestBody List<Long> ids){
        bookService.deleteAllBook(ids);
        return "成功";
    }

    @PutMapping("/put")
    public String editBook(@RequestBody Book books){
        return bookService.editBook(books);
    }


    @GetMapping("/findId/{ids}")
    public Book findIdBook(@PathVariable Long ids){
        return bookService.findIdBook(ids);
    }


    @GetMapping("/findAllId")
    public List<Book> findAllByIdBook(@RequestBody List<Long> ids){
        return bookService.findAllByIdBook(ids);
    }

    @GetMapping("/find")
    public List<Book> findAllBook(){
        return bookService.findAllBook();
    }


    @GetMapping("/author/")
    public List<Book> getBooksByAuthorStartingWith(@Param("author") String author){
        return bookService.getBooksByAuthorStartingWith(author);

    }

    @GetMapping("/price/")
    public List<Book> getBooksByPriceGreaterThan(@Param("price") Float price){
        return bookService.getBooksByPriceGreaterThan(price);
    }

    @GetMapping("/maxId")
    public Book getMaxIdBook(){
        return bookService.getMaxIdBook();
    }

    @GetMapping("/IdAndAuthor/")
    public List<Book> getBookByIdAndAuthor(@Param("author") String author, @Param("id") Long id){
        return bookService.getBookByIdAndAuthor(author, id);
    }

    @GetMapping("/IdAndName/")
    public List<Book> getBooksByIdAndName(@Param("name") String name, @Param("id") Long id){
        return bookService.getBooksByIdAndName(name, id);
    }

    @GetMapping("/pageable/")
    public Page<Book> getBookByPage(@Param("page") Pageable pageable){
        return bookService.getBookByPage(pageable);
    }

    @GetMapping("/page")
    public List<Book> getBookByPage(int pageNo, int size){
        return bookService.getBookByPage(pageNo, size);
    }

}
